#include "friendorgroupinformwidget.h"
#include "ui_friendorgroupinformwidget.h"
#include "Core/tcpclient.h"
#include "someItem/friendorgroupitem.h"
#include "Core/messagedispatcher.h"

FriendOrGroupInformWidget::FriendOrGroupInformWidget(long long MSG_TYPE_INFO,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FriendOrGroupInformWidget)
{
    ui->setupUi(this);
    InitForUi();
    MsgTypeInfo=MSG_TYPE_INFO;
    LinkUiToUiSiganlAndSlot();
    LinkTcpSignalAndSlot();
    setAttribute(Qt::WA_DeleteOnClose, true);

}

FriendOrGroupInformWidget::~FriendOrGroupInformWidget()
{
    delete ui;
}

void FriendOrGroupInformWidget::InitForUi()
{
    this->setFixedSize(520,600);
    MainLayout = new QVBoxLayout(this);

    MainLayout->setContentsMargins(0, 0, 0, 0);
    Head = new QWidget;
    Head->setFixedHeight(25);
    Head->setStyleSheet("background: white; border: 1px solid #eee;");
    MainLayout->addWidget(Head);

    QScrollArea* scrollArea = new QScrollArea(this);
    scrollArea->setWidgetResizable(true);
    scrollArea->setStyleSheet("QScrollArea { border: 1px solid #ccc; }");

    QWidget* BodyScrollWidget = new QWidget;
    scrollArea->setWidget(BodyScrollWidget);

    // 内容布局
    BodyLayout = new QVBoxLayout(BodyScrollWidget);
    BodyLayout->setSpacing(10); // 项之间间距
    BodyLayout->setContentsMargins(10, 10, 10, 10);

    MainLayout->addWidget(scrollArea, 1);

}

void FriendOrGroupInformWidget::LinkUiToUiSiganlAndSlot()
{

}

void FriendOrGroupInformWidget::LinkTcpSignalAndSlot()
{
    if(MsgTypeInfo==MSG_TYPE_FRIEND_INFORM_PULL_REQUEST){
        connect(this,&FriendOrGroupInformWidget::Sig_FriendInformPull,
                TcpClient::get_instance(),&TcpClient::SendMsg);

        connect(MessageDispatcher::GetInstance()->getFriendServer(),
                &FriendService::EmitFriendInformList,
                this,&FriendOrGroupInformWidget::OnSetFriendInformListItem);

        QJsonObject Info;
        Info["MASTER"]=TcpClient::get_instance()->getUserName();
        Info["NUM"]=10;
        emit Sig_FriendInformPull(MSG_TYPE_FRIEND_INFORM_PULL_REQUEST,Info);
    }
    else if(MsgTypeInfo==MSG_TYPE_GROUP_INFORM_PULL_REQUEST){
        connect(this,&FriendOrGroupInformWidget::Sig_GroupInformPull,
                TcpClient::get_instance(),&TcpClient::SendMsg);
        //待实现
        emit Sig_GroupInformPull(0,QJsonObject());
    }
}

void FriendOrGroupInformWidget::OnSetFriendInformListItem(QList<QMap<QString, QVariant> > InfoList)
{
    qDebug()<<__FILE__<<__LINE__<<InfoList;
    for(int i=0;i<InfoList.size();i++){
        if(InfoList[i]["RequestStatus"].value<double>()!=FRIEND_REQUEST_STATUS_WAIT_DEAL)continue;

        FriendOrGroupItem * Ainfo=new FriendOrGroupItem;
        Ainfo->setNotificationInfo("",InfoList[i]["SenderId"].value<QString>(),
        {},{},MSG_TYPE_FRIEND_INFORM_PULL_REQUEST);
        BodyLayout->addWidget(Ainfo);

    }
    BodyLayout->addItem(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));
}
